home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / expand.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  2KB  |  115 lines

  1. /*
  2.  * Do filename expansion with the shell.  Returns a pointer to a
  3.  * static area.
  4.  */
  5.  
  6. #define EXPAND_BUF    2048
  7.  
  8. #include <stdio.h>
  9.  
  10. char *
  11. expand(input)
  12. char *input;
  13. {
  14.     FILE *fp, *popen();
  15.     int last;
  16.     char buf[256], *strpbrk(), *strcpy();
  17.     static char ans[EXPAND_BUF];
  18.  
  19.     if (input == NULL)
  20.         return(NULL);
  21.     if (*input == '\0')
  22.         return("");
  23.                     /* any thing to expand? */
  24.     if (!strpbrk(input, "$*{}[]\\?~")) {
  25.         strcpy(ans, input);
  26.         return(ans);
  27.     }
  28.                     /* popen an echo */
  29.     sprintf(buf, "echo %s", input);
  30.  
  31.     fp = popen(buf, "r");
  32.     fgets(ans, EXPAND_BUF, fp);
  33.     pclose(fp);
  34.  
  35.     if (!strlen(ans)) {
  36.         strcpy(ans, input);
  37.         return(ans);
  38.     }
  39.  
  40.     /*
  41.      * A horrible kludge...  if the last character is not a line feed,
  42.      * then the csh has returned an error message.  Otherwise zap the
  43.      * line feed.
  44.      */
  45.     last = strlen(ans) - 1;
  46.     if (ans[last] != '\n') {
  47.         strcpy(ans, input);
  48.         return(ans);
  49.     }
  50.     else
  51.         ans[last] = '\0';
  52.  
  53.     return(ans);
  54. }
  55.  
  56. /*
  57.  * Miscellaneous routines probably missing from Bezerkely
  58.  */
  59.  
  60. #ifdef BSD
  61. /*
  62.  * Return ptr to first occurrence of any character from `brkset'
  63.  * in the character string `string'; NULL if none exists.
  64.  */
  65.  
  66. char *
  67. strpbrk(string, brkset)
  68. register char *string, *brkset;
  69. {
  70.     register char *p;
  71.  
  72.     if (!string || !brkset)
  73.         return(0);
  74.     do {
  75.         for (p = brkset; *p != '\0' && *p != *string; ++p)
  76.             ;
  77.         if (*p != '\0')
  78.             return(string);
  79.     }
  80.     while (*string++);
  81.     return(0);
  82. }
  83.  
  84. /*
  85.  * Copies the character c, n times to string s
  86.  */
  87.  
  88. char *
  89. memset(s, c, n)
  90. char *s, c;
  91. int n;
  92. {
  93.     char *s1 = s;
  94.  
  95.     while (n > 0) {
  96.         --n;
  97.         *s++ = c;
  98.     }
  99.     return(s1);
  100. }
  101.  
  102. /*
  103.  * Copy contents of memory (with possible overlapping).
  104.  */
  105.  
  106. char *
  107. memcpy(s1, s2, n)
  108. char *s1, *s2;
  109. int n;
  110. {
  111.     bcopy(s2, s1, n);
  112.     return(s1);
  113. }
  114. #endif /* BSD */
  115.